using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IGCal.GA;
using IGCal.Office.Spreadsheet;
using IGCal.Funzioni;
using IGCal.Office.Writer;


namespace Calcio
{
    public partial class frmGareStampa2 : Form
    {
        public frmGareStampa2()
        {
            InitializeComponent();
        }

        private void btnStampa_Click(object sender, EventArgs e)
        {
            StampaParte();
        }

        private void StampaParte()
        {
            string SQL = "";
            SQL += "Select * ";
            SQL += "From "; 
            SQL += "Gare ";
            SQL += "Where ";
            SQL += "Data >= "+ Globals.Connection.SQLDate(new DateTime(2007,1,1));
            
            DataTable dt = Globals.Connection.GetDataTable(SQL);

            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("Nessuna dato della lista d'attesa da stampare", Globals.ProjectName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                dt.Dispose();
                return;
            }

            IGCal.Office.Spreadsheet.IApplication xlApp = null;
            if (Settings.MachineSettings.ReportOutputType == ReportOutputType.MSOffice)
                xlApp = new IGCal.Office.MSO.Excel.Application(false);
            else if (Settings.MachineSettings.ReportOutputType == ReportOutputType.OpenOffice)
                xlApp = new IGCal.Office.OO.Spreadsheet.Application();
            else
                return;
            IWorkbook wbk = xlApp.Workbooks.Add();

            ISheet sht = null;

            // Provo ad assegnare alla variabile sht il primo Sheet del Workbook
            try
            { sht = wbk.Sheets[0]; }
            // Se va in errore (non esiste lo Sheets[0]), creo un nuovo sheet e lo aggiungo al workbook
            catch
            { sht = wbk.Sheets.Add(); }

            // Imposto l'orientamento della pagina
            sht.PageSetup.Orientation = IGCal.Office.Common.PageOrientation.Landscape;
            

            int rowNumber =2;
                      
            sht.Cells[rowNumber, 2].Text = string.Format("Squadra Di Casa");
           sht.Cells[rowNumber, 2].Font.Size = 18;
           sht.Cells[rowNumber, 2].Font.Bold = true;
           sht.Columns[2].IsAutoWidth = true;
           
            sht.Cells[rowNumber, 3].Text = string.Format("Squadra Ospite");
           sht.Cells[rowNumber, 3].Font.Size = 18;
           sht.Cells[rowNumber, 3].Font.Bold = true;
           sht.Columns[3].IsAutoWidth = true;

            sht.Cells[rowNumber, 4].Text = string.Format("Risultato Finale");
           sht.Cells[rowNumber, 4].Font.Size = 18;
           sht.Cells[rowNumber, 4].Font.Bold = true;
           sht.Cells[rowNumber, 4].HorizontalAlignment = IGCal.Office.Common.HorizontalAlignment.Center;
           sht.Columns[4].HorizontalAlignment = IGCal.Office.Common.HorizontalAlignment.Center;
           sht.Columns[4].IsAutoWidth = true;
           
           sht.Cells[rowNumber, 5].Text = string.Format("Squadra Ospite");
           sht.Cells[rowNumber, 5].Font.Size = 18;
           sht.Cells[rowNumber, 5].Font.Bold = true;
           sht.Columns[5].IsAutoWidth = true;
           sht.Columns[5].HorizontalAlignment = IGCal.Office.Common.HorizontalAlignment.Right;
            
            rowNumber++;
            foreach ( DataRow dr in dt.Rows)
            {
                sht.Cells[rowNumber, 2].Text = string.Format(General.DBToString(dr["SquadraDiCasa"]));
                sht.Cells[rowNumber, 3].Text = string.Format(General.DBToString(dr["SquadraOspite"]));
                sht.Cells[rowNumber, 4].Text = string.Format(General.DBToString(dr["RisultatoFinale"]));
                sht.Cells[rowNumber, 5].Text = string.Format(General.DBToString(dr["Data"]));
                rowNumber++;
            }

            sht.Range(2, 2, rowNumber - 1, 5).LeftBorder.Weight = IGCal.Office.Common.BorderWeight.Thick;
            sht.Range(2, 2, rowNumber - 1, 5).RightBorder.Weight = IGCal.Office.Common.BorderWeight.Thick;
            sht.Range(2, 2, rowNumber - 1, 5).TopBorder.Weight = IGCal.Office.Common.BorderWeight.Thick;
            sht.Range(2, 2, rowNumber - 1, 5).BottomBorder.Weight = IGCal.Office.Common.BorderWeight.Thick;

            sht.Range(2, 2, 2, 5).BottomBorder.Weight = IGCal.Office.Common.BorderWeight.Thick;
            sht.Range(2, 2, 2, 5).BackColor = Color.GreenYellow.ToArgb();

            sht.Range(3, 2, rowNumber - 1, 5).BackColor = Color.LightSeaGreen.ToArgb();


            sht.Name = "prova";
            
            
            xlApp.Visible = true;
            
            dt.Dispose();
        
        }

        internal void Start()
        {
            LegaProcedureAdEventi();

            this.MdiParent = Globals.MDIForm;
            this.Show();
            this.BringToFront();
        }

        private void LegaProcedureAdEventi()
        {
            btnStampa.Click += new EventHandler(btnStampa_Click);
            btnStampa2.Click += new EventHandler(btnStampa2_Click);
        }

        void btnStampa2_Click(object sender, EventArgs e)
        {
            string SQL = "";
            SQL += "Select * ";
            SQL += "From ";
            SQL += "Gare ";
            SQL += "Where ";
            SQL += "Data >= " + Globals.Connection.SQLDate(new DateTime(2007, 1, 1));


            DataTable tbl = Globals.Connection.GetDataTable(SQL);
            IGCal.Office.Writer.IApplication wrdApp = FunzioniGA.GetWriterApplication();

            
            //if (Settings.MachineSettings.ReportOutputType == ReportOutputType.MSOffice)
            //    wrdApp = new IGCal.Office.MSO.Word.Application();
            //else if (Settings.MachineSettings.ReportOutputType == ReportOutputType.OpenOffice)
            //    wrdApp = new IGCal.Office.OO.Writer.Application();
            //else
            //    return;
            
            IDocument wrdDoc = null;
            try
            { 
                wrdDoc = wrdApp.Documents.Add("");
        
            }
                
            catch (Exception ex)
            {
                IGCal.Windows.DB.frmStampaErroriDB.Start(string.Format("Impossibile aprire modello '{0}'.", ""), ex);
                wrdApp = null;
                return;
            }

            
            wrdDoc.Tables.Add(30,4);

            int colonna = 0;
            int riga = 0;
            foreach (DataRow dd in tbl.Rows)
            {
                colonna = 0;
                wrdDoc.Tables[0].Cells[riga, colonna].BackColor = Color.LightGreen.ToArgb();
                wrdDoc.Tables[0].Cells[riga, colonna].Text = string.Format(General.DBToString(dd["SquadraDiCasa"]));
                    colonna++;
                    wrdDoc.Tables[0].Cells[riga, colonna].BackColor = Color.Green.ToArgb();
                wrdDoc.Tables[0].Cells[riga, colonna].Text = string.Format(General.DBToString(dd["SquadraOspite"]));
                colonna++;
                if (String.Compare(dd["Esito"].ToString(),"Vittoria")==0)
                    wrdDoc.Tables[0].Cells[riga, colonna].Font.Bold = true;
                wrdDoc.Tables[0].Cells[riga, colonna].BackColor = Color.LightGreen.ToArgb();
                wrdDoc.Tables[0].Cells[riga, colonna].Text = string.Format(General.DBToString(dd["RisultatoFinale"]));
                colonna++;
                wrdDoc.Tables[0].Cells[riga, colonna].BackColor = Color.Green.ToArgb();
                wrdDoc.Tables[0].Cells[riga, colonna].Text = string.Format(General.DBToString(dd["Data"]));
                riga++;
            
            }


            DataRow dr = tbl.Rows[0];
            
            try
            {
                if (dr["SquadraDiCasa"] == DBNull.Value)
                    wrdDoc.Bookmarks["bkmSquadraDiCasa"].Text = "";
                else
                {
                    wrdDoc.Bookmarks["bkmSquadraDiCasa"].Text = dr["SquadraDiCasa"].ToString();
                    wrdDoc.Bookmarks["bkmSquadraOspite"].Text = dr["SquadraOspite"].ToString();
                    wrdDoc.Bookmarks["bkmData"].Text = string.Format(General.DBToString(dr["Data"]));
                    wrdDoc.Bookmarks["bkmRisultatoFinale"].Text = string.Format(General.DBToString(dr["RisultatoFinale"]));
                }
                }
            catch { }

                wrdApp.Visible = true;
        }
    }
}